import axios from 'axios'
class HttpRequest {
    constructor(baseUrl) {
        this.baseUrl = baseUrl
    }
    getInsideConfig() {
        const config = {
            baseURL: this.baseUrl,
            method: 'post',
            headers: {}
        }
        return config
    }
    destroy(url) {
        delete this.queue[url]
        if (!Object.keys(this.queue).length) {
            // Spin.hide()
        }
    }
    interceptors(instance, url) {
        // 请求拦截
        instance.interceptors.request.use(
            config => {
                // 添加全局的loading...
                if (!Object.keys(this.queue).length) {
                    // Spin.show() // 不建议开启，因为界面不友好
                }
                this.queue[url] = true
                return config
            },
            error => {
                return Promise.reject(error)
            }
        )
        // 响应拦截
        instance.interceptors.response.use(
            res => {
                this.destroy(url)
                this.errMsg(res)
                const { data, status, statusText } = res
                return { data, status, statusText }
            },
            error => {
                this.destroy(url)
                this.errMsg(error)
                let errorInfo = error.response
                if (!errorInfo) {
                    const { statusText, status, config } = error
                    errorInfo = {
                        statusText,
                        status: status || -1,
                        request: { responseURL: config.url }
                    }
                }
                return errorInfo
                // return Promise.reject(error)
            }
        )
    }
    request(options) {
        const instance = axios.create()
        options = Object.assign(this.getInsideConfig(), options)
        this.interceptors(instance, options.url)
        return instance(options)
    }
    errMsg(err) {
        if (err.response) {
            switch (err.response.status) {
                case 400:
                    err.response.statusText = '错误请求'
                    break
                case 401:
                    err.response.statusText = '未授权，请重新登录'
                    break
                case 403:
                    err.response.statusText = '拒绝访问'
                    break
                case 404:
                    err.response.statusText = '请求错误,未找到该资源'
                    break
                case 405:
                    err.response.statusText = '请求方法未允许'
                    break
                case 408:
                    err.response.statusText = '请求超时'
                    break
                case 500:
                    err.response.statusText = '服务器端出错'
                    break
                case 501:
                    err.response.statusText = '网络未实现'
                    break
                case 502:
                    err.response.statusText = '网络错误'
                    break
                case 503:
                    err.response.statusText = '服务不可用'
                    break
                case 504:
                    err.response.statusText = '网络超时'
                    break
                case 505:
                    err.response.statusText = 'http版本不支持该请求'
                    break
                default:
                    err.response.statusText = `连接错误${err.response.status}`
            }
        } else if (err.status != 200) {
            err.statusText = '连接到服务器失败'
        }
    }
}
export default HttpRequest
